package com.wyapples.kittcar;

import java.util.ArrayList;
import java.util.Random;

public class Kitt {

	private static String[] ANSWER_HELLO = { "Yes, sir?", "Hello, sir.",
			"You look nice sir.", "Waiting for your command." };
	private static String[] ANSWER_ASK_ORDER = { "Give me the order, sir.",
			"Waiting for your command, sir.", "Speak please, I'm listening." };
	private static String[] ANSWER_ACTION_UNCOMPLETED_LENGTH = {
			"How long, sir?", "How many meters should I go?",
			"Roger, and the length please." };
	private static String[] ANSWER_ACTION_UNCOMPLETED_DEGREE = {
			"The angle, please.", "What's the angle?",
			"How many degrees should I turn?" };
	private static String[] ANSWER_ACTION_COMPLETED = { "More actions?",
			"More commands?", "One more action?", "Any other action?" };
	private static String[] ANSWER_CONFIRM = { "Are you sure?", "Is it ok?",
			"Confirm that, sir?", "Am I right?" };
	private static String[] ANSWER_UNKNOW = { "Sorry sir, I didn't hear you.",
			"Pardan?", "Something wrong with my ear, could you repeat it?",
			"What?" };
	private static String[] ANSWER_MOVING = { "Moving.", "On my way, sir.",
			"Go! go! go!" };

	private static final Random RANDOM = new Random();

	private String whatToSay;
	private ArrayList<Command> commandsList = new ArrayList<Command>();
	private Command currentCommand, tmpCommand;
	public boolean readyToGo;

	public int beTold(ArrayList<String> recivedArrayList) {
		whatToSay = null;
		tmpCommand = new Command(recivedArrayList);
		int type = tmpCommand.getType();

		if (type == Command.COMMAND_TYPE_UNKNOW) {
			handleUnknow();
		} else if (type == Command.COMMAND_TYPE_HELLO) {
			handleHello();
		} else if (type == Command.COMMAND_TYPE_ACTION) {
			handleAction(tmpCommand);
		} else if (type == Command.COMMAND_TYPE_PARAMETER) {
			handleParameter(tmpCommand);
		} else if (type == Command.COMMAND_TYPE_CONFIRM) {
			handleConfirm(tmpCommand);
		}

		return type;
	}

	public void handleHello() {
		int answerLength = ANSWER_HELLO.length;
		whatToSay = ANSWER_HELLO[RANDOM.nextInt(answerLength)];
	}

	public void handleUnknow() {
		int answerLength = ANSWER_UNKNOW.length;
		whatToSay = ANSWER_UNKNOW[RANDOM.nextInt(answerLength)];
	}

	public void handleAction(Command recivedCommand) {
		currentCommand = recivedCommand;

		if (recivedCommand.isCompleted()) {
			int answerLength = ANSWER_ACTION_COMPLETED.length;
			whatToSay = ANSWER_ACTION_COMPLETED[RANDOM.nextInt(answerLength)];
		} else {
			int action = recivedCommand.getCommand();

			if (action == Command.ACTION_TOWARD
					|| action == Command.ACTION_BACKWORD) {
				int answerLength = ANSWER_ACTION_UNCOMPLETED_LENGTH.length;
				whatToSay = ANSWER_ACTION_UNCOMPLETED_LENGTH[RANDOM
						.nextInt(answerLength)];
			} else if (action == Command.ACTION_LEFT
					|| action == Command.ACTION_RIGHT) {
				int answerLength = ANSWER_ACTION_UNCOMPLETED_DEGREE.length;
				whatToSay = ANSWER_ACTION_UNCOMPLETED_DEGREE[RANDOM
						.nextInt(answerLength)];
			}
		}
	}

	public void handleParameter(Command recivedCommand) {
		if (currentCommand == null
				|| currentCommand.getType() != Command.COMMAND_TYPE_ACTION) {

		} else {
			currentCommand.setParameter(recivedCommand.getParameter());
			int answerLength = ANSWER_ACTION_COMPLETED.length;
			whatToSay = ANSWER_ACTION_COMPLETED[RANDOM.nextInt(answerLength)];
		}

	}

	public void handleConfirm(Command recivedCommand) {
		if (currentCommand == null || !currentCommand.isCompleted()) {

		} else {
			commandsList.add(currentCommand);

			if (recivedCommand.getCommand() == Command.CONFIRM_TRUE) {
				currentCommand = null;
				int answerLength = ANSWER_ASK_ORDER.length;
				whatToSay = ANSWER_ASK_ORDER[RANDOM.nextInt(answerLength)];
			} else {
				int answerLength = ANSWER_MOVING.length;
				whatToSay = ANSWER_MOVING[RANDOM.nextInt(answerLength)];
				readyToGo = true;
			}
		}
	}

	public String getWhatToSay() {
		return whatToSay;
	}

	public ArrayList<Command> getCommands() {
		return commandsList;
	}

	public void cleanUp() {
		commandsList.clear();
		currentCommand = null;
	}
}
